package cn.xiaou.view;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import cn.xiaou.service.UserService;
import cn.xiaou.service.impl.UserServiceImpl;
import cn.xiaou.utils.FontUtils;
import cn.xiaou.utils.ImageManagerUtils;
/*
 * 修改密码，未发现bug
 */
public class ForgetPasswordView extends JFrame implements ActionListener,FocusListener{
	private static final long serialVersionUID = 1L;
	private CardLayout card = new CardLayout();
	private JPanel father = new JPanel();
	private UserService userService = new UserServiceImpl();
	private JButton back = new JButton("返回");
	//用于临时存储
	private Integer modify_id=-1;
	private String newPasswordStr;
	//--------------------p1---------------------------
	private JButton username = new JButton("记得用户名");
	private JButton email = new JButton("记得邮箱");
	//--------------------p2---------------------------
	private JButton checkUsername = new JButton("验证用户名");
	private JTextField inUsername = new JTextField();
	private JButton back2 = new JButton("返回");
	//--------------------p3---------------------------
	private JButton checkEmail = new JButton("验证电子邮箱");
	private JTextField inEmail = new JTextField();
	private JButton back3 = new JButton("返回");
	//--------------------p4---------------------------
	private JButton checkCode = new JButton("验证激活码");
	private JTextField code = new JTextField();
	private JButton back4 = new JButton("返回");
	//--------------------p5---------------------------
	private JButton modifyOk = new JButton("完成修改");
	private JButton modifyBack = new JButton("放弃");
	private JTextField newPassword = new JTextField();
	private JTextField newRePassword = new JTextField();
	public ForgetPasswordView() {
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				new LoginView();
				dispose();
			 }
		});
		JPanel p  =  new JPanel();
		JPanel p1 = new JPanel();
		JPanel p2 = new JPanel();
		JPanel p3 = new JPanel();
		JPanel p4 = new JPanel();
		JPanel p5 = new JPanel();
		setIconImage(getToolkit().getImage(ImageManagerUtils.icoUrl));
		setResizable(false);
		add(father);
		setTitle("修改密码");
		//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		father.setLayout(card);
		father.add(p,"index");
		father.add(p2,"username");
		father.add(p3,"email");
		father.add(p4,"code");
		father.add(p5,"modify");
		setSize(400, 200);
		setVisible(true);
		setLocationRelativeTo(null);
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {  
				super.windowClosing(e);  
				if(modify_id!=-1) {
					userService.updateState(modify_id);
				}
			}  
		});
			
	//--------------------p1------------------------
		p.setLayout(new BorderLayout());
		JLabel l1 = new JLabel("修改密码，一个用户一天5次修改机会",JLabel.CENTER);
		//l1.setPreferredSize(new Dimension(400, 50));
		p.add(l1,BorderLayout.NORTH);
		p.add(back,BorderLayout.SOUTH);
		p1.setLayout(new GridLayout(1,2));
		p1.add(username);
		p1.add(email);
		p.add(p1);
		back.addActionListener(this);
		username.addActionListener(this);
		email.addActionListener(this);
	//------------------p2----------------------------
		p2.setLayout(null);
		p2.add(inUsername);
		p2.add(checkUsername);
		p2.add(back2);
		inUsername.setForeground(new Color(192,192,192));
		inUsername.setText("请输入用户名");
		inUsername.setBounds(70, 50, 240, 30);
		checkUsername.setBounds(130, 90, 120, 35);
		back2.setBounds(320, 125, 62, 30);
		inUsername.addFocusListener(this);
		checkUsername.addActionListener(this);
		back2.addActionListener(this);
	//-----------------p3-----------------------------
		p3.setLayout(null);
		p3.add(inEmail);
		p3.add(checkEmail);
		p3.add(back3);
		inEmail.setForeground(new Color(192,192,192));
		inEmail.setText("请输入电子邮箱");
		inEmail.setBounds(70, 50, 240, 30);
		checkEmail.setBounds(130, 90, 120, 35);
		back3.setBounds(320, 125, 62, 30);
		inEmail.addFocusListener(this);
		checkEmail.addActionListener(this);
		back3.addActionListener(this);
	//----------------p4-----------------------------
		p4.setLayout(null);
		p4.add(code);
		p4.add(checkCode);
		p4.add(back4);
		code.setForeground(new Color(192,192,192));
		code.setText("请输入激活码，已经发送到电子邮箱");
		code.setBounds(70, 50, 240, 30);
		checkCode.setBounds(130, 90, 120, 35);
		back4.setBounds(320, 125, 62, 30);
		code.addFocusListener(this);
		inEmail.addFocusListener(this);
		checkCode.addActionListener(this);
		back4.addActionListener(this);
	//---------------p5------------------------------
		p5.setLayout(null);
		p5.add(newPassword);
		p5.add(newRePassword);
		p5.add(modifyOk);
		p5.add(modifyBack);
		newPassword.setForeground(new Color(192,192,192));
		newRePassword.setForeground(new Color(192,192,192));
		newPassword.setText("请输入密码");
		newRePassword.setText("请再一次输入密码");
		newPassword.setBounds(70, 25, 240, 30);
		newRePassword.setBounds(70, 58, 240, 30);
		modifyOk.setBounds(130, 100, 120, 35);
		modifyBack.setBounds(320, 125, 62, 30);
		newPassword.addFocusListener(this);
		newRePassword.addFocusListener(this);
		modifyOk.addActionListener(this);
		modifyBack.addActionListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==username) {
			card.show(father,"username");
		}else if(e.getSource() == email) {
			card.show(father, "email");
		}else if(e.getSource() == checkUsername && inUsername.getText().matches("[a-zA-Z0-9]+")) {
			if(!userService.isExistUserByUsername(inUsername.getText())) {
				modify_id = userService.modifyPasswordByUsername(inUsername.getText());
				if(modify_id==-1) {
					Object[] options = { "确定"}; 
					JOptionPane.showOptionDialog(null, "未知错误1", "警告", 
							JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, 
							null, options,options[0]); 
					username.setText("");
					card.show(father, "index");
				}else {
					card.show(father, "code");
				}
			}else {
				Object[] options = { "点击重新输入"}; 
				JOptionPane.showOptionDialog(null, "用户名不存在", "警告", 
						JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, 
						null, options,options[0]); 
				username.setText("");
			}
		}else if(e.getSource() == checkEmail && inEmail.getText().matches("^(\\w)+(\\.\\w+)*@(\\w)+((\\.\\w+)+)$")) {
			if(!userService.checkExistEmail(inEmail.getText())) {
				modify_id = userService.modifyPasswordByEmail(inEmail.getText());
				if(modify_id==-1) {
					Object[] options = { "确定"}; 
					JOptionPane.showOptionDialog(null, "未知错误", "警告", 
							JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, 
							null, options,options[0]); 
					username.setText("");
					card.show(father, "index");
				}else {
					card.show(father, "code");
				}
				
			}else {
				Object[] options = { "点击重新输入"}; 
				JOptionPane.showOptionDialog(null, "电子邮箱不存在", "警告", 
						JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, 
						null, options,options[0]);
				
			}
		}else if(e.getSource() == checkCode && !code.getText().equals("请输入激活码，已经发送到电子邮箱")) {
			String codeStr = code.getText();
			Boolean codeState=userService.checkModifyPasswordCodeByCode(codeStr,modify_id);
			if(codeState) {
				card.show(father, "modify");
			}else{
				Object[] options = { "确定"}; 
				JOptionPane.showOptionDialog(null, "验证码错误，用户名或电子邮箱", "警告", 
						JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, 
						null, options,options[0]); 
				username.setText("");
				card.show(father, "index");
			}
			
		}else if(e.getSource()==back2 || e.getSource()==back3 ) {
			card.show(father, "index");
		}else if(e.getSource() == modifyBack||e.getSource()==back) {
			if(modify_id!=-1) {
				userService.updateState(modify_id);
			}
			new LoginView();
			dispose();
		}else if(e.getSource() == modifyOk && "**********************".equals(newRePassword.getText())) {
			Boolean state = userService.modifyPasswordByModifyId(modify_id,newPasswordStr);
			if(!state) {
				Object[] options = { "确定"}; 
				JOptionPane.showOptionDialog(null, "未知错误2", "警告", 
						JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, 
						null, options,options[0]);
				if(modify_id!=-1) {
					userService.updateState(modify_id);
				}
				card.show(father, "index");
			}else {
				Object[] options = { "确定"}; 
				JOptionPane.showOptionDialog(null, "密码修改完成", "提示", 
						JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, 
						null, options,options[0]);
				new LoginView();
				dispose();
			}
		}else if(e.getSource()==back4 ){
			if(modify_id!=-1) {
				userService.updateState(modify_id);
			}
			card.show(father, "index");
		}
			
	}

	@Override
	public void focusGained(FocusEvent e) {
		if(e.getSource() instanceof JTextField) {
			((JTextField) e.getSource()).setFont(FontUtils.font1);
			((JTextField) e.getSource()).setText("");
			((JTextField) e.getSource()).setForeground(Color.BLACK);
		}
	}

	@Override
	public void focusLost(FocusEvent e) {
		if(e.getSource()==inUsername) {
			if(inUsername.getText().equals("")) {
				inUsername.setForeground(new Color(192,192,192));
				inUsername.setText("请输入用户名");
				return;
			}
			if(!inUsername.getText().matches("[a-zA-Z0-9]+")) {
				inUsername.setForeground(new Color(255,0,0));
				inUsername.setText("用户名存在非法字符");
				return;
			}
		}else if(e.getSource() == inEmail) {
			if(inEmail.getText().equals("")) {
				inEmail.setForeground(new Color(192,192,192));
				inEmail.setText("请输入电子邮箱");
				return;
			}
			if(!inEmail.getText().matches("^(\\w)+(\\.\\w+)*@(\\w)+((\\.\\w+)+)$")) {
				inEmail.setForeground(new Color(192,192,192));
				inEmail.setText("请输入正确的电子邮箱格式");
				return;
			}
		}else if(e.getSource() == code) {
			if(inEmail.getText().equals("")) {
				inEmail.setForeground(new Color(192,192,192));
				inEmail.setText("请输入验证码");
				return;
			}
		}else if(e.getSource() == newPassword) {
			if(newPassword.getText().equals("")) {
				newPassword.setForeground(new Color(192,192,192));
				newPassword.setText("请输入密码");
				return;
			}
			if(newPassword.getText().length()<6||newPassword.getText().length()>12) {
				newPassword.setForeground(new Color(255,0,0));
				newPassword.setText("密码小于6位或大于12位");
				return;
			}
			newPasswordStr = newPassword.getText();
			if(!newPasswordStr.replaceAll("^[\\u4e00-\\u9fa5]*$","1213").equals(newPassword.getText())) {
				newPassword.setForeground(new Color(255,0,0));
				newPassword.setText("请勿输入中文");
				return;
			}
			newPassword.setText("**********************");
		}else if(e.getSource() == newRePassword) {
			if(newRePassword.getText().equals("")) {
				newRePassword.setForeground(new Color(192,192,192));
				newRePassword.setText("请再一次输入密码");
				return;
			}
			if(!newPasswordStr.equals(newRePassword.getText())) {
				newRePassword.setForeground(new Color(255,0,0));
				newRePassword.setText("密码输入不一致");
				return;
			}
			newRePassword.setText("**********************");
		}
		
		
	}
}
